//
//  BBSMineSenderViewController.m
//  GuoMedical
//
//  Created by 张育铭 on 2019/10/24.
//  Copyright © 2019 深圳大愚信息科技有限公司. All rights reserved.
//

#import "BBSMineSenderViewController.h"
#import "CJTableViewModel.h"
#import "BBSViewListCell.h"

@interface BBSMineSenderViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView  *tableView;
/// 回复
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger totalCount;


@end

@implementation BBSMineSenderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(244, 244, 244);
    [self setNavBarTitle:@"健康论坛"];
    _totalCount = 1;
    [self setupRefreshData];
    [self setNavBarRightBtn:[GHOCNavBarView createImgNaviBarBtnByImgNormal:@"luntanshare" imgHighlight:nil target:self action:@selector(rightBarButtonClick:)]];
    
    
    [self downLodingDate];
    [self.view addSubview:[self headViewUI]];
    [self.view addSubview:self.tableView];
}
- (void)rightBarButtonClick:(UIButton*)sender {
    
}

- (void)downLodingDate {
    [[HTTPManager sharedInstance] GetWithPath:@"/frontend/news/forum/mine" parametersDict:@{@"page_size":@"20",@"page":[NSString stringWithFormat:@"%ld",(long)self.totalCount]} sucessBlock:^(NSURLSessionDataTask *operation, id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] integerValue] == 0) {
            [self.dataArray    removeAllObjects];
            for (NSDictionary *dic in responseObject[@"data"][@"items"]) {
                CJTableViewModel *model = [[CJTableViewModel alloc]initWithDict:dic];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } failtBlock:^(NSURLSessionDataTask *operation, id error) {
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.dataArray[indexPath.row];
    return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[BBSViewListCell class] contentViewWidth:SCREEN_WIDTH];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   BBSViewListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   if (cell == nil) {
       cell = [[BBSViewListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   }
   [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
   cell.model = self.dataArray[indexPath.row];
   cell.coreTextClick = ^{
       [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
   };
   return cell;
}
/// 设置头视图
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
/// 头视图风格
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = RGBCOLOR(244, 244, 244);
    return view;
}
- (UIView*)headViewUI {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_TabbarHeight, SCREEN_WIDTH, 80)];
    headView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *thePeoplePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 70, 70)];
    thePeoplePhoto.layer.cornerRadius  = 35;
    thePeoplePhoto.layer.masksToBounds = YES;
    thePeoplePhoto.contentMode = UIViewContentModeScaleAspectFill;
     
    return headView;
}
- (void)setupRefreshData {
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.totalCount = 1;
            [self downLodingDate];
            [weakSelf.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.totalCount ++;
            [self downLodingDate];
            [weakSelf.tableView.mj_footer endRefreshing];
        });
    }];
}
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, SCREEN_TabbarHeight+80, SCREEN_WIDTH, SCREEN_HEIGHT-SCREEN_TabbarHeight)style:(UITableViewStyleGrouped)];
        _tableView.delegate        = self;
        _tableView.dataSource      = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorColor  = [UIColor clearColor];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}
- (NSMutableArray*)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
